package com.aiju.cash.ui.good;

import android.content.Intent;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.aiju.base.activity.BaseTitleActivity;
import com.aiju.bean.Good;
import com.aiju.bean.GoodsType;
import com.aiju.cash.R;
import com.aiju.cash.core.api.DataJson;
import com.aiju.cash.core.api.GoodApi;
import com.aiju.cash.ui.good.fragment.GoodFragment;
import com.aiju.cash.ui.good.fragment.GoodManagerFragment;
import com.aiju.utils.T;
import com.aiju.widgit.StateButton;
import com.aiju.widgit.dialog.MemberDialog;
import com.zhy.http.okhttp.callback.StringCallback;

import java.util.List;

import okhttp3.Call;

/**
 * @author: 佐为
 * @date: 2016-11-16 10:30
 * @version: V1.0
 * @Description: 商品主页
 */
public class GoodActivity extends BaseTitleActivity implements View.OnClickListener {

    //全局视图
    private boolean isDelete;
    private GoodManagerFragment goodFragment;
    private TextView managerTextView;
    private StateButton addTextView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_good);
        setCenterTitle("商品管理");
        if (mUser.getRole().equals("0") && mUser.getShop().getShopType().equals("0")) {
            mAiJuTitleBarView.setRightTitle("编辑");
            mAiJuTitleBarView.setRightOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mAiJuTitleBarView.getRightTitle().equals("编辑")) {
                        mAiJuTitleBarView.setRightTitle("完成");
                        managerTextView.setText("取消");
                        addTextView.setText("删除");
                        isDelete = true;
                        goodFragment.setDelete(isDelete);

                        managerTextView.setBackgroundResource(R.drawable.shape_btn_ec4545);
                        managerTextView.setTextColor(Color.parseColor("#ec4545"));
                        addTextView.setNormalBackgroundColor(Color.parseColor("#ec4545"));
                        addTextView.setPressedBackgroundColor(Color.parseColor("#ec4545"));
                    } else {
                        mAiJuTitleBarView.setRightTitle("编辑");
                        managerTextView.setText("分类管理");
                        addTextView.setText("新增商品");
                        isDelete = false;
                        goodFragment.setDelete(isDelete);
                        managerTextView.setBackgroundResource(R.drawable.shape_btn_display);
                        managerTextView.setTextColor(getResources().getColor(R.color.app_display_color));
                        addTextView.setNormalBackgroundColor(getResources().getColor(R.color.app_display_color));
                        addTextView.setPressedBackgroundColor(getResources().getColor(R.color.app_display_color));
                    }
                }
            });
        }

        addTextView = (StateButton) findViewById(R.id.btn_good_add);
        managerTextView = (TextView) findViewById(R.id.btn_category_manager);
        addTextView.setOnClickListener(this);
        managerTextView.setOnClickListener(this);

        if (mUser.getRole().equals("0") && mUser.getShop().getShopType().equals("0")) {
            findViewById(R.id.ly_shop_bottom).setVisibility(View.VISIBLE);
        }else {
            findViewById(R.id.ly_shop_bottom).setVisibility(View.GONE);
        }

        Bundle bundle = new Bundle();
        bundle.putInt("show", GoodFragment.DELETE);
        goodFragment = new GoodManagerFragment();
        goodFragment.setArguments(bundle);
        getSupportFragmentManager().beginTransaction().add(R.id.fl_good_content, goodFragment).commit();
    }

    @Override
    public void onClick(View v) {
      /*  if (!mUser.getRole().equals("0") && mUser.getShop().getManageOwnGoods().equals("0")) {
            showLimitsDailog("无法创建类目及商品", "您的店铺尚未开启管理权限，无法创建和管理自己的商品，请联系您的管理员。");
            return;
        }*/
        switch (v.getId()) {
            case R.id.btn_category_manager:
                managerClick();
                break;
            case R.id.btn_good_add:
                addClick();
                break;
        }
    }

    /**
     * 提示联系管理员
     */
    private void showLimitsDailog(String title, String msg) {
        new MemberDialog(this)
                .setLayoutParams()
                .setTitileText(title)
                .setMsgText(msg)
                .setLeftText("取消")
                .setRightText("联系管理员")
                .setOnSimpleClick(new MemberDialog.OnSimpleClick() {
                    @Override
                    public void onCancel() {

                    }

                    @Override
                    public void onSubmit() {
                        Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + mUser.getCreatorPhone()));
                        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        GoodActivity.this.startActivity(intent);
                    }
                }).show();
    }

    /**
     * 管理点击
     */
    private void managerClick() {
        if (isDelete) {//返回
            mAiJuTitleBarView.setRightTitle("编辑");
            managerTextView.setText("分类管理");
            addTextView.setText("新增商品");
            isDelete = false;
            goodFragment.setDelete(isDelete);

            managerTextView.setBackgroundResource(R.drawable.shape_btn_display);
            managerTextView.setTextColor(getResources().getColor(R.color.app_display_color));
            addTextView.setNormalBackgroundColor(getResources().getColor(R.color.app_display_color));
            addTextView.setPressedBackgroundColor(getResources().getColor(R.color.app_display_color));
        } else {//分类管理
            Intent intent = new Intent(this, GoodCategoryActivity.class);
            intent.putExtra("type", 0);
            startActivity(intent);
        }
    }

    //添加点击
    private void addClick() {
        if (isDelete) {//删除
            goodDelete();
        } else {//新增商品
            startGoodAddActivity();
        }
    }

    private void goodDelete() {
        List<GoodsType> goodsTypes = goodFragment.getGoodsTypes();
        StringBuilder sb = new StringBuilder();
        for (GoodsType type : goodsTypes) {
            List<Good> selectGoods = type.getSelectGoods();
            for (Good good : selectGoods) {
                sb.append(good.getId());
                sb.append(",");
            }
        }
        if (sb.length() == 0) {
            T.ToastShort("未选中任何商品");
            return;
        }
        sb.deleteCharAt(sb.length() - 1);
        new GoodApi().goodDelete(sb.toString(), mUser.getToken(), new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                T.ToastNetError();
            }

            @Override
            public void onResponse(String response, int id) {
                DataJson json = new DataJson(response);
                if (json.isSuccess()) {
                    T.ToastShort("成功删除所选商品");
                    goodFragment.deleteChange();
                } else {
                    T.ToastShort(json.getMsg());
                }
            }
        });
    }

    /**
     * 跳转商品添加界面
     */
    private void startGoodAddActivity() {
        Intent intent = new Intent(this, GoodAddActivity.class);
        intent.putExtra("type", 0);

        //intent.putExtra("position", goodFragment.getTypePosition());
        intent.putExtra("position", goodFragment.getGoodType());
        startActivity(intent);
    }
}



